package com.fatmeraid.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fatmeraid.common.exception.FarmException;
import com.fatmeraid.common.login.LoginUser;
import com.fatmeraid.common.login.LoginUserHolder;
import com.fatmeraid.common.model.PageResult;
import com.fatmeraid.common.result.ResultCodeEnum;
import com.fatmeraid.dao.PolicyDao;
import com.fatmeraid.dao.UserDao;
import com.fatmeraid.domain.Policy;
import com.fatmeraid.domain.User;
import com.fatmeraid.service.PolicyService;

import com.fatmeraid.vo.PaginationVo;
import com.fatmeraid.vo.PolicyVo;
import org.apache.ibatis.session.ResultContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class PolicyServiceImpl implements PolicyService {

	@Autowired
	private PolicyDao policyDao;
	@Autowired
	private UserDao userDao;
	@Override
	public PageResult<Policy> getPolicyList(PaginationVo paginationVo) {
		// 分页查询
		IPage pade = new Page(paginationVo.getCurrent(),paginationVo.getSize());
		// 创建查询条件
		LambdaQueryWrapper<Policy> lqw = new LambdaQueryWrapper<>();

		lqw.eq(Policy::getStatus,1);

		policyDao.selectPage(pade,lqw);
		return new PageResult<Policy>(pade.getTotal(),pade.getRecords());

	}

	@Override
	public Policy getPolicyDetail(Long policyId) {
		Policy policy = policyDao.selectById(policyId);
		return policy;
	}

	@Override
	public boolean addPolicy(Policy policy) {
		int insert = policyDao.insert(policy);
		if (insert <= 0){
			throw new FarmException(ResultCodeEnum.FAIL);
		}
		return true;
	}

	@Override
	public boolean updatePolicy(Policy policy) {
		int insert = policyDao.updateById(policy);
		if (insert <= 0){
			throw new FarmException(ResultCodeEnum.FAIL);
		}
		return true;
	}

	@Override
	public boolean deletePolicy(Long policyId) {
		int insert = policyDao.deleteById(policyId);
		if (insert <= 0){
			throw new FarmException(ResultCodeEnum.FAIL);
		}
		// 逻辑删除
		return true;
	}

	// 管理员获取政策列表
	@Override
	public PageResult<Policy> getAdminPolicyList(PaginationVo paginationVo) {
		// 分页查询
		IPage pade = new Page(paginationVo.getCurrent(),paginationVo.getSize());
		// 创建查询条件
		LambdaQueryWrapper<Policy> lqw = new LambdaQueryWrapper<>();
		policyDao.selectPage(pade,null);
		return new PageResult<Policy>(pade.getTotal(),pade.getRecords());
	}
}